package com.lcx.movie.controller;

import com.lcx.movie.common.Rep;
import com.lcx.movie.entity.MovieDetail;
import com.lcx.movie.service.MovieDetailService;
import io.minio.MinioClient;
import io.minio.errors.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * @author 李丽君
 * @version 1.0
 * @Description 电影海报控制层
 * @date 2025/2/21 9:58
 */
@RestController
@CrossOrigin(origins = "*")
public class MovieDetailController {
    @Autowired
    private MovieDetailService movieDetailService;

    /**
     * 新增多张电影海报
     * @param movieDetails 电影海报实体
     * @return 字符串提示信息
     */
    @PostMapping("/insertImg")
    public String insertImg(List<MovieDetail> movieDetails) {
        // 调用新增图片方法
        int addImgList = movieDetailService.addImgList(movieDetails);
        // 判断
        if (addImgList > 0) {
            return "success";
        }else {
            return "fail";
        }
    }
    @Autowired
    MinioClient minioClient;

    /**
     * 删除电影海报方法
     * @param detailId 电影海报id
     * @param fileName 文件名
     * @return 字符串提示信息
     */
    @GetMapping("/deleteImg")
    public String deleteImg(@Param("detailId") Integer detailId,@Param("fileName") String fileName) {
        System.out.println("detailId================:"+detailId);
        try {
            boolean isExists=minioClient.bucketExists("moviebucket");
            if (!isExists){
                return "桶不存在";
            }
            // 调用删除方法
            int i = movieDetailService.deleteImg(detailId);
            if(i>0){
                minioClient.removeObject("moviebucket",fileName);
            }
            // 删除minio里面的对应的文件
            return "删除成功";
        } catch (Exception e) {
            //throw new RuntimeException(e);
            return "删除失败";
        }
    }
    /**
     * 根据电影id查询出多张电影海报
     * @param movieId 电影id
     * @return 查出的电影海报集合
     */
    @GetMapping("/findManyImgs")
    public Rep findManyImgs(@Param("movieId") int movieId){
        List<MovieDetail> manyImgs = this.movieDetailService.findManyImgs(movieId);
        return new Rep().addData("manyImgs", manyImgs);
    }
}
